package com.fms.controller;

import com.fms.constant.MessageConstant;
import com.fms.dto.JsonRequest;
import com.fms.result.ResultData;
import com.fms.security.CheckUtils;
import com.fms.service.AttendanceTaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/attendanceTask")
@Slf4j
public class AttendanceTaskController {
    private final AttendanceTaskService attendanceTaskService;

    private final CheckUtils checkUtils;

    @PostMapping("/create")
    public ResultData createAttendanceTask(@RequestBody JsonRequest.AttendanceTaskRequest attendanceTaskRequest){
        log.info("新增签到任务：{}",attendanceTaskRequest);
        //时间校验
        Map<String,LocalTime> timeMap=Map.of(
                MessageConstant.START_TIME,attendanceTaskRequest.getStartTime(),
            MessageConstant.IN_DEADLINE, attendanceTaskRequest.getInDeadline(),
            MessageConstant.OUT_DEADLINE, attendanceTaskRequest.getOutDeadline()
        );
        checkUtils.checkIsUseDeptIdOfAttendanceTask(attendanceTaskRequest.getDeptIds(),null,timeMap);
        attendanceTaskService.createAttendanceTask(attendanceTaskRequest);
        return ResultData.success("新建任务成功");
    }

    @GetMapping("/pageList")
    public ResultData pageListAttendanceTask(JsonRequest.AttendanceTaskPageRequest attendanceTaskPageRequest){
        log.info("分页查询签到任务：{}",attendanceTaskPageRequest);
        return ResultData.success("查询任务成功",attendanceTaskService.pageListAttendanceTask(attendanceTaskPageRequest));
    }

    @DeleteMapping("/delete/{id}")
    public ResultData deleteAttendanceTask(@PathVariable Long id){
        log.info("删除签到任务{}",id);
        checkUtils.checkTaskIdOfAttendanceUpdate(id);
        attendanceTaskService.deleteAttendanceTaskById(id);
        return ResultData.success("删除任务成功");
    }

    @PutMapping("/update/{id}")
    public ResultData updateAttendanceTask(@PathVariable Long id,@RequestBody JsonRequest.AttendanceTaskRequest attendanceTaskRequest){
        log.info("更新签到任务{}",attendanceTaskRequest);
        checkUtils.checkTaskIdOfAttendanceUpdate(id);
        //时间校验
        Map<String,LocalTime> timeMap=Map.of(
                MessageConstant.START_TIME,attendanceTaskRequest.getStartTime(),
                MessageConstant.IN_DEADLINE, attendanceTaskRequest.getInDeadline(),
                MessageConstant.OUT_DEADLINE, attendanceTaskRequest.getOutDeadline()
        );
        checkUtils.checkIsUseDeptIdOfAttendanceTask(attendanceTaskRequest.getDeptIds(),id,timeMap);
        attendanceTaskService.updateAttendanceTask(id,attendanceTaskRequest);
        return ResultData.success("更新任务成功");
    }

    @PutMapping("/disable/{id}")
    public ResultData disableAttendanceTask(@PathVariable Long id){
        log.info("禁用签到任务{}",id);
        checkUtils.checkTaskIdOfAttendanceUpdate(id);
        attendanceTaskService.disableAttendanceTask(id);
        return ResultData.success("禁用任务成功");
    }

    @PutMapping("/enable/{id}")
    public ResultData enableAttendanceTask(@PathVariable Long id){
        log.info("启用签到任务{}",id);
        attendanceTaskService.enableAttendanceTask(id);
        return ResultData.success("启用任务成功");
    }

    @PutMapping("/end/{id}")
    public ResultData endAttendanceTask(@PathVariable Long id){
        log.info("结束签到任务{}",id);
        checkUtils.checkTaskIdOfAttendanceEnd(id);
        attendanceTaskService.endAttendanceTask(id);
        return ResultData.success("结束任务成功");
    }

}
